package com.vite.sse.controller;

import com.vite.sse.service.SseService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@RestController
@RequiredArgsConstructor
public class SseController {

    private final SseService sseService;

    /**
     * 建立SSE连接
     * @param userName 用户名
     * @return SseEmitter 连接对象
     */
    @GetMapping(value = "/sse/connect", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter connect(@RequestParam("userName") String userName) {
        return sseService.connect(userName);
    }

}